package cn.codesea.housework_user.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.codesea.housework_user.R;
import cn.codesea.housework_user.adapter.HomeRcvAdapter;
import cn.codesea.housework_user.adapter.listener.OnRecyclerItemClickActivityListener;
import cn.codesea.housework_user.entity.Activity;
import cn.codesea.housework_user.net.ServiceGenerator;
import cn.codesea.housework_user.net.data.Result;
import cn.codesea.housework_user.net.service.ActivityService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class AcListActivity extends AppCompatActivity{


    @BindView(R.id.toolbar)
    Toolbar toolbar;

    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout mSwipeRefreshLayout;
    List<Activity> acts = new ArrayList<>();
    private HomeRcvAdapter mHomeRcvAdapter;
    private LinearLayoutManager mLinearLayoutManager;

    @BindView(R.id.search_iv)
    ImageView search_iv;
    @BindView(R.id.search_et)
    EditText search_et;

    int userid = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_aclist);
        ButterKnife.bind(this);
        init();
    }

    public void init(){
        initData();
        initView();
    }

    public void initView(){

        // 初始化 toolbar
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        initRcl();
    }

    @OnClick(R.id.search_iv)
    public void search_iv(){

        ActivityService activityService = ServiceGenerator.createService(ActivityService.class);
        Call<Result> call = activityService.searchActs(search_et.getText().toString(),userid);
        call.enqueue(new Callback<Result>(){
            @Override
            public void onResponse(Call<Result> call, Response<Result> response) {
                resultProcess(response.body());
            }

            @Override
            public void onFailure(Call<Result> call, Throwable t) {

            }
        });
    }

    /**
     * 初始化 列表控件
     */
    public void initRcl(){

        mHomeRcvAdapter = new HomeRcvAdapter(this,acts);
        mHomeRcvAdapter.setRecyclerItemClickListener(new OnRecyclerItemClickActivityListener() {
            @Override
            public void onItemClick(int Position, List<Activity> list) {
//                Toast.makeText(AcListActivity.this,"Position:"+ Position, Toast.LENGTH_SHORT).show();
                startActivity(new Intent(AcListActivity.this,AcActivity.class).putExtra("act",acts.get(Position)));
            }
        });
        mLinearLayoutManager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);

        mRecyclerView.setLayoutManager(mLinearLayoutManager);
        mRecyclerView.setAdapter(mHomeRcvAdapter);

        initListener();
    }

    /**
     * 获取所有活动
     */
    private void getActs() {
        ActivityService activityService = ServiceGenerator.createService(ActivityService.class);
        Call<Result> call = activityService.getActs();
        call.enqueue(new Callback<Result>(){
            @Override
            public void onResponse(Call<Result> call, Response<Result> response) {
                resultProcess(response.body());
            }

            @Override
            public void onFailure(Call<Result> call, Throwable t) {

            }
        });
    }

    /**
     * 获取已参加的活动
     */
    private void getJoinedActs(){
        ActivityService activityService = ServiceGenerator.createService(ActivityService.class);
        Call<Result> call = activityService.joinActsSearch(userid);
        call.enqueue(new Callback<Result>(){
            @Override
            public void onResponse(Call<Result> call, Response<Result> response) {
                resultProcess(response.body());
            }

            @Override
            public void onFailure(Call<Result> call, Throwable t) {

            }
        });
    }

    private void initData(){

        userid = getIntent().getIntExtra("userid",-1);
        if (userid != -1){
            getJoinedActs();
        }else {
            getActs();
        }

    }

    private void resultProcess(Result result){
        Type type = new TypeToken<List<Activity>>(){}.getType();
        acts.clear();
        acts.addAll((List<Activity>)new Gson().fromJson(String.valueOf(result.getRes()),type));
        mHomeRcvAdapter.notifyDataSetChanged();
    }

    private void initListener() {
        initPullRefresh();
//        initLoadMoreListener();
    }

    private void initPullRefresh() {
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

                ActivityService activityService = ServiceGenerator.createService(ActivityService.class);
                Call<Result> call = activityService.getActs();
                call.enqueue(new Callback<Result>(){
                    @Override
                    public void onResponse(Call<Result> call, Response<Result> response) {
                        Result result = response.body();
                        Type type = new TypeToken<List<Activity>>(){}.getType();
                        acts.clear();
                        acts.addAll((List<Activity>)new Gson().fromJson(String.valueOf(result.getRes()),type));
                        mHomeRcvAdapter.notifyDataSetChanged();

                        mSwipeRefreshLayout.setRefreshing(false);
                        Toast.makeText(AcListActivity.this, "刷新成功！", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onFailure(Call<Result> call, Throwable t) {

                    }
                });

            }
        });
    }
}
